<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/Country.class.php');
require_once(SITE_ROOT . 'class/bean/Division.class.php');
require_once(SITE_ROOT . 'class/bean/League.class.php');

class DBLeague {
	
	public function getLeaguesByDivision($division) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `country#`, `division#` FROM `tleague` WHERE `country#` = ? AND `division#` = ?')) {
			$stmt->bind_param('ii', $countryID, $divisionID);
			$countryID = $division->getCountry()->getCountryID();
			$divisionID = $division->getDivisionID();
			$stmt->execute();
			$stmt->bind_result($leagueID, $name, $countryID, $divisionID);
			
			$leagues = new ArrayObject();
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
				$leagues->append($league);
			}			
			$stmt->close();
			$db->disconnect();
			
			return $leagues;
		}
	}
	
	public function getLeaguesByLevel($level) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `country#`, `division#` FROM `tleague` WHERE `division#` = ?')) {
			$stmt->bind_param('i', $level);
			$stmt->execute();
			$stmt->bind_result($leagueID, $name, $countryID, $divisionID);
			
			$leagues = new ArrayObject();
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
				$leagues->append($league);
			}			
			$stmt->close();
			$db->disconnect();
			
			return $leagues;
		}
	}
	
	public function getLeague($leagueID) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `country#`, `division#` FROM `tleague` WHERE `league#` = ?')) {
			$stmt->bind_param('i', $leagueID);
			$stmt->execute();
			$stmt->bind_result($leagueID, $name, $countryID, $divisionID);
			
			$league = null;
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
			}			
			$stmt->close();
			$db->disconnect();
			
			return $league;
		}
	}
	
	public function saveLeagues($leagues) {
		$db = new DB;
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `tleague` (`country#`, `division#`, `league#`, `name`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `country#`=VALUES(`country#`), `division#`=VALUES(`division#`)')) {
			foreach ($leagues as $league) {
				$stmt->bind_param('iiis', $countryID, $divisionID, $leagueID, $name);
				
				$countryID = $league->getDivision()->getCountry()->getCountryID();
				$divisionID = $league->getDivision()->getDivisionID();
				$leagueID = $league->getLeagueID();
				$name = $league->getName();
				
				$stmt->execute();
			}
			$stmt->close();
			$db->disconnect();
		}
	}
	
	public function deleteLeaguesByCountry($country) {
		$db = new DB;
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('DELETE FROM `tleague` WHERE `country#` = ?')) {
			$stmt->bind_param('i', $countryID);
			$countryID = $country->getCountryID();
			$stmt->execute();
			$stmt->close();
			$db->disconnect();
		}
	}
		
}

?>